//
//  ViewController.m
//  Rac
//
//  Created by 崔博 on 2017/12/28.
//  Copyright © 2017年 崔博. All rights reserved.
//  组合。保证信号 响应的顺讯

#import "ViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import "LoginViewModel.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *accoundFiled;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activity;
@property (strong, nonatomic)  LoginViewModel *loginViewModel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.activity.hidden = YES;
    _loginViewModel = [LoginViewModel new];
    RAC(self.loginViewModel, userName) = self.accoundFiled.rac_textSignal;
    RAC(self.loginViewModel, password) = self.accoundFiled.rac_textSignal;
    self.loginBtn.rac_command = self.loginViewModel.loginCommand;
    [[self.loginViewModel.loginCommand executionSignals]
     subscribeNext:^(RACSignal *x) {
         
         self.activity.hidden = NO;
         [x subscribeNext:^(NSString *x) {
             self.activity.hidden = YES;
             NSLog(@"%@",x);
         }];
     }];
}


- (void)text{
    RACSubject * sub = [RACSubject subject];
    
    // skip:跳跃几个值
    [[sub skip:2] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    
    // 请求回来
    [sub sendNext:@{@"str":@"1"}];
    [sub sendNext:@{@"11":@"1"}];
    [sub sendNext:@{@"str":@"1"}];
    [sub sendNext:@{@"11":@"1"}];
    [sub sendNext:@{@"str":@"1"}];
    [sub sendNext:@{@"11":@"1"}];
}
- (void)distinctUntilChanged{
    // 创建信号
    RACSubject * sub = [RACSubject subject];
    
    // 忽略重复数据
    [[sub distinctUntilChanged] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    
    // 请求回来
    [sub sendNext:@{@"str":@"1"}];
    [sub sendNext:@{@"11":@"1"}];
}
- (void)take{
    // take:
    RACSubject * sub = [RACSubject subject];
    // 标记信号
    RACSubject *signal = [RACSubject subject];
    // take指定拿前几条数据
    // takeLast 从后往前注意(一定写要结束)
    // takeUntil 直到你的标记信号发送数据的时候结束
    [[sub takeUntil:signal] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    
    [sub sendNext:@"1"];
    [signal sendNext:@".."];
    [sub sendNext:@"2"];
    [sub sendNext:@"3"];
}
- (void)ingore{
    //    ignore
    RACSubject * sub = [RACSubject subject];
    
    // 忽略一些值
    RACSignal * ingoreSinga = [sub ignore:@"1"];
    
    [ingoreSinga subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    
    
    [sub sendNext:@"1"];
}
- (void)fiter{
    [[_accoundFiled.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
        return YES;
    }] subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"%@",x);
    }];
}
- (void)conbine{
    // 组合 reduce 有参数
    RACSignal * singal = [RACSignal combineLatest:@[_accoundFiled.rac_textSignal,_pwdField.rac_textSignal] reduce:^id _Nullable(NSString * account,NSString * pwd){
        return @(account.length && pwd.length);
    }];
    
    RAC(_loginBtn,enabled) = singal;
}
@end
